#coding=utf-8

import sys
import HTMLParser
import urlparse
import urllib
import urllib2
import cookielib
import string
import re

def postCurl(url,data):
    # data - dict
    data = urllib.urlencode(data)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    return response.read()

def getCurl(url,data):
        # data - dict
        data = urllib.urlencode(data)
        url = url + '?' + data
        response = urllib2.urlopen(url)
        return  response.read()

def getPureCurl(url):
        response = urllib2.urlopen(url)
        return  response.read()


data1 = {
        'Company' : '万加111',
        'Password' : 'iwilley',
        'UserName' : 'manager',
        '__RequestVerificationToken' : '5467uX1Xbi1fS8R7_0pubDzl3K4bpYxdoXUoExRSy3Lgcv9ZcO3T_IgJTB88E4oBIEhEf2ZCi9x7xqNq3mIs0O5q2g7MjUZlGFXroeCJ2s71Dt_bptH-oryTcDhSQH56wy1lNP8S_RHdzF2oMthVdg2'
}
url = "http://www.24th.com/customer/account/loginPost/"
data = {
    'login[password]' : 'anscssss',
    'login[rememberme]' : 1,
    'login[username]' : 'aaa',
    'send' : '',
}
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
           'Referer' : 'http://v2245.ants-city.com/Account/Login?ReturnUrl=%2f'}
cj = cookielib.CookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
postData = urllib.urlencode(data)
request = urllib2.Request(url, postData)
response = urllib2.urlopen(request)
text = response.read()
print text

for item3 in cj:
    print(item3.name)
    print(item3.value)

#html = postCurl(url,data)
#print(html)

print('=============================')

hosturl = 'http://v2245.ants-city.com/'
#posturl = 'http://v2245.ants-city.com/Account/Login?ReturnUrl=%2F'
posturl = 'http://v2245.ants-city.com/Account/Login'

#设置一个cookie处理器，它负责从服务器下载cookie到本地，并且在发送请求时带上本地的cookie
#cj = cookielib.LWPCookieJar()
cj = cookielib.CookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)


#打开登录主页面（他的目的是从页面下载cookie，这样我们在再送post数据时就有cookie了，否则发送不成功）
h = urllib2.urlopen(hosturl)
print(h.read())
print('=============================')
v = ''
for item in cj:
    if(item.name == '__RequestVerificationToken'):
        v = item.value

#构造header，一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
           'Referer' : 'http://v2245.ants-city.com/Account/Login?ReturnUrl=%2f'}
#构造Post数据，他也是从抓大的包里分析得出的。
postData = {'Company' : '万加111',
            'Password' : 'iwilley',
            'UserName' : 'manager',
            '__RequestVerificationToken' : v
            }


#需要给Post数据编码
postData = urllib.urlencode(postData)

#通过urllib2提供的request方法来向指定Url发送我们构造的数据，并完成登录过程
request = urllib2.Request(posturl, postData, headers)
#print request
response = urllib2.urlopen(request)
text = response.read()
print text

for item2 in cj:
    print(item2.name)
    print(item2.value)

url = "http://v2245.ants-city.com/DRP/DRPFItemStockReport/GetData?action=read&page=1&start=0&limit=30&sort=[{%22property%22%3A%22Available%22%2C%22direction%22%3A%22DESC%22}]&filter=[{%22property%22%3A%22ItemInfo%22%2C%22value%22%3A%22111%22}]"
#html = getPureCurl(url)
html = urllib2.urlopen(url)
print(html)

print('=============================')
